//
//  PickerView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/11.
//

import SwiftUI


//时间滚轮
struct PickTimeView: View {
    let title : String
    @Binding var time : Int
    let onDismiss: () -> Void
    
    private let hours = 0...23
    private let minutes = 0...59
    
    
    // 分别创建小时和分钟的 @State 属性来跟踪用户的选择
    @State private var selectedHour =  0
    @State private var selectedMinute = 0
    
    var body: some View {
        ZStack(alignment:.top) {
            PopupBottomView(title: title) {
                HStack{
                    Picker("选择小时", selection: $selectedHour) {
                        ForEach(hours, id: \.self) { hour in
                            Text("\(hour)时")
                        }
                    }
                    .pickerStyle(.wheel)
                    Picker("选择分", selection: $selectedMinute) {
                        ForEach(minutes, id: \.self) { minute in
                            Text(String(format: "%02d", minute) + "分")
                        }
                    }
                }
                .pickerStyle(.wheel)
            }
            HStack {
                Spacer()
                Text("确定")
                    .font(.system(size: 14))
                    .foregroundStyle(.white)
                    .padding(EdgeInsets(top: 5, leading: 7, bottom: 5, trailing: 7))
                    .background(themeManager.themeColor)
                    .cornerRadius(5)
                    .onTapGesture {
                        time = selectedHour * 3600 + selectedMinute * 60
                        onDismiss()
                    }
            }.padding(10)
        }.onAppear{
            // 在视图出现时设置初始值
            selectedHour = time / 3600
            selectedMinute = (time % 3600) / 60
        }
    }
    
}

#Preview {
    //    PickerView(options:genderOptions,selectId: "0") {}
}
